<template>
	<view class="container">
		<view class="example">
			<u--form labelPosition="left" :rules="rules" :model="user" ref="uForm" labelWidth="100">
				<u-form-item label="用户姓名" prop="name" required>
					<u--input v-model="user.name" clearable placeholder="请输入姓名"></u--input>
				</u-form-item>
				<u-form-item label="手机号码" prop="mobile" required>
					<u--input v-model="user.mobile" clearable placeholder="请输入手机号"></u--input>
				</u-form-item>
			<!-- 	<u-form-item label="性别" prop="gender" @click="showSex = true; hideKeyboard()" required>
					<u-input style="pointer-events: none;" v-model="user.gender" disabled disabledColor="#ffffff"
						placeholder="请选择性别">
						<u-icon slot="suffix" name="arrow-right" color="rgb(192, 196, 204)" size="20"></u-icon>
					</u-input>

				</u-form-item> -->
				<!-- 	<u-form-item label="车间部门">
					<u--input v-model="user.compName" readonly border="none"></u--input>
				</u-form-item> -->
			</u--form>
			<button style="margin-top: 24px;" type="primary" @click="submit">提交</button>
		</view>

		<u-action-sheet :show="showSex" :actions="actions" title="请选择性别" @close="showSex = false" @select="sexSelect">
		</u-action-sheet>
	</view>
</template>

<script>
	import {
		updateUser
	} from '@/common/api.js'
	export default {
		data() {
			return {
				showSex: false,


				user: {
					name: "",
					mobile: "",
					gender: "",
					compName: "",
				},
				actions: [{
						name: '男',
					},
					{
						name: '女',
					},
				],
				rules: {
					name: {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					gender: {
						type: 'string',
						max: 1,
						required: true,
						message: '请选择性别',
						trigger: ['blur', 'change']
					},
					mobile: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},

			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
			// console.log(this.userInfo, 'this.userInfo');
			this.user.name = this.userInfo.name
			this.user.mobile = this.userInfo.mobile
			this.user.gender = this.userInfo.gender
			this.user.compName = this.userInfo.compName
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {

			submit() {

				this.$refs.uForm.validate().then(res => {
					updateUser(this.user).then(ree => {
						if (ree.code == '00000') {
							this.$commonFn.msg('修改成功')
							uni.setStorageSync('userInfo', ree.data);
						}
					})
				})



			},
			sexSelect(e) {
				this.user.gender = e.name
			},
		}
	}
</script>

<style lang="scss" scoped>
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>